package com.cloud.xtools.module.controller.admin;

import com.cloud.xtools.common.controller.BaseController;
import com.cloud.xtools.common.response.ApiResponse;
import com.cloud.xtools.common.response.PageList;
import com.cloud.xtools.common.validator.annotation.EntityExist;
import com.cloud.xtools.module.domain.entity.Module;
import com.cloud.xtools.module.domain.query.admin.ModuleQuery;
import com.cloud.xtools.module.domain.request.admin.ModuleCreateRequest;
import com.cloud.xtools.module.domain.request.admin.ModuleUpdateRequest;
import com.cloud.xtools.module.domain.resource.admin.ModuleResource;
import com.cloud.xtools.module.service.admin.ModuleService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/admin")
@RequiredArgsConstructor
@Validated
public class ModuleController extends BaseController {
    private final ModuleService moduleService;

    @GetMapping("modules")
    public ApiResponse<PageList> list(@Valid @ModelAttribute ModuleQuery moduleQuery) {
        return success(moduleService.moduleList(moduleQuery));
    }

    @PostMapping("modules")
    public ApiResponse<ModuleResource> add(@Valid @RequestBody ModuleCreateRequest moduleCreateRequest) {
        return created(moduleService.add(moduleCreateRequest));
    }

    @PutMapping("modules/{id}")
    public ApiResponse<ModuleResource> edit(@EntityExist(entity = Module.class) @PathVariable Long id, @Valid @RequestBody ModuleUpdateRequest moduleUpdateRequest) {
        moduleService.edit(id, moduleUpdateRequest);
        return success();
    }
}
